package com.clothing.customization.service.impl;

import com.clothing.customization.entity.DesignerSchedule;
import com.clothing.customization.repository.DesignerScheduleRepository;
import com.clothing.customization.service.DesignerScheduleService;
import jakarta.persistence.criteria.Predicate;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class DesignerScheduleServiceImpl implements DesignerScheduleService {

    private final DesignerScheduleRepository scheduleRepository;

    @Override
    public Page<DesignerSchedule> findByPage(Map<String, Object> params) {
        int page = Integer.parseInt(Objects.toString(params.getOrDefault("page", "1")));
        int size = Integer.parseInt(Objects.toString(params.getOrDefault("size", "10")));
        
        // 创建查询条件
        Specification<DesignerSchedule> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            
            // 设计师姓名
            if (params.get("designerName") != null && !params.get("designerName").toString().isEmpty()) {
                predicates.add(cb.like(root.get("designer").get("realName"), "%" + params.get("designerName") + "%"));
            }
            
            // 日程类型
            if (params.get("scheduleType") != null && !params.get("scheduleType").toString().isEmpty()) {
                predicates.add(cb.equal(root.get("scheduleType"), params.get("scheduleType")));
            }
            
            // 日期范围
            if (params.get("dateRange") != null && !params.get("dateRange").toString().isEmpty()) {
                String[] dateRange = params.get("dateRange").toString().split(",");
                if (dateRange.length == 2) {
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date startDate = sdf.parse(dateRange[0]);
                        Date endDate = sdf.parse(dateRange[1]);
                        predicates.add(cb.greaterThanOrEqualTo(root.get("scheduleDate"), startDate));
                        predicates.add(cb.lessThanOrEqualTo(root.get("scheduleDate"), endDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            return cb.and(predicates.toArray(new Predicate[0]));
        };
        
        // 创建分页请求
        PageRequest pageRequest = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "scheduleDate"));
        
        return scheduleRepository.findAll(spec, pageRequest);
    }

    @Override
    @Transactional
    public void cancel(Long id) {
        DesignerSchedule schedule = scheduleRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("日程记录不存在"));
        
        schedule.setStatus(3); // 已取消
        schedule.setCancelTime(new Date());
        
        scheduleRepository.save(schedule);
    }
} 